package com.youtui.manager.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.youtui.manager.common.ServiceException;

/** controller异常统一拦截处理 */
public class MyExceptionHandler implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// 根据不同错误转向不同页面
		if (ex instanceof UnauthorizedException) {
			return new ModelAndView("unauthorizedUrl");
		}
		if (ex instanceof AuthorizationException) {
			return new ModelAndView("unauthorizedUrl");
		}
		response.setContentType("text/html; charset=UTF-8");
		if (ex instanceof ServiceException) {
			// service异常
			try {
				//Msg msg = Msg.init(Msg.STATUS_EXCEPTION, Msg.EXEPTION);
				response.getWriter().write("");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (ex instanceof Exception) {
			// 普通异常
			try {
				//Msg msg = Msg.init(Msg.STATUS_EXCEPTION, Msg.EXEPTION);
				response.getWriter().write("");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
